﻿using Jiz.WebApi;
using JzSport.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Jiz.Account.WebApi
{
    public class LoginByPhoneAction : PostAction<LoginByPhoneParams, LoginByPhoneResult>
    {
        protected override ActionResult<LoginByPhoneResult> OnExecute(ActionContext context, SysParameters sysParameters, LoginByPhoneParams opParams)
        {
            AccountService service = new AccountService();
            ServiceResult<LoginResult> serviceResult = service.LoginByPhone(opParams.Phone, opParams.Password);

            if (serviceResult.ErrorCode == ErrorCode.NoError)
            {
                LoginByPhoneResult actionResult = new LoginByPhoneResult()
                {
                    Id = serviceResult.Data.UserId,
                    Name = serviceResult.Data.Name,
                    Phone = serviceResult.Data.Phone,
                    Token = UIDToken.ToToken(u.build(serviceResult.Data.UserId, DateTime.Now.AddYears(100).ToString("yyyy-MM-dd HH:mm:ss"))),
                    Sex = serviceResult.Data.Sex,
                    AvatarUrl = string.Format("{0}{1}", Configs.BaseWebUrl, serviceResult.Data.AvatarUrl),
                    NickName = serviceResult.Data.NickName,
                };

                actionResult.City = serviceResult.Data.City;

                //JizSport.Common.Logger.DebugToTag(LogTag.TAG, "LoginByPhone success, userid:{0}, name:{1}, phone:{2}", serviceResult.Data.UserId, serviceResult.Data.Name, serviceResult.Data.Phone);
                return new ActionResult<LoginByPhoneResult>("1.0.3",
                    true,
                    ErrorCode.NoError,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
            }
            else
            {
                LoginByPhoneResult actionResult = new LoginByPhoneResult();
                if (serviceResult.Data != null)
                {
                    actionResult.Id = serviceResult.Data.UserId;
                    actionResult.Name = serviceResult.Data.Name;
                    actionResult.Phone = serviceResult.Data.Phone;
                    actionResult.RetryCount = serviceResult.Data.RetryCount;
                    actionResult.LockoutDuration = serviceResult.Data.LockoutDuration;
                    actionResult.Sex = serviceResult.Data.Sex;
                    actionResult.NickName = serviceResult.Data.NickName;
                    actionResult.AvatarUrl = string.Format("{0}{1}", Configs.BaseWebUrl, serviceResult.Data.AvatarUrl);
                }
                return new ActionResult<LoginByPhoneResult>("1.0.3",
                    false,
                    serviceResult.ErrorCode,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
            }
        }
    }

    public class LoginByPhoneParams
    {
        [Parameter(Namespace="op")]
        public string Phone { get; set; }

        [Parameter(Namespace = "op")]
        public string Password { get; set; }
    }

    public class LoginByPhoneResult
    {
        /// <summary>
        /// 编号
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 电话
        /// </summary>
        public string Phone { get; set; }

        /// <summary>
        /// 认证标志
        /// </summary>
        public string Token { get; set; }

        /// <summary>
        /// 密码重试的次数，当次数等于0时账号将被锁定
        /// </summary>
        public int RetryCount { get; set; }

        /// <summary>
        /// 账号锁定时间（分钟）
        /// </summary>
        public int LockoutDuration { get; set; }

        /// <summary>
        /// 性别
        /// </summary>
        public int Sex { get; set; }

        /// <summary>
        /// 头像地址
        /// http://...
        /// </summary>
        public string AvatarUrl { get; set; }

        public string NickName { get; set; }

        public string City { get; set; }
    }
}
